package org.zizi.action.service;

import org.zizi.action.ActionService;
import org.zizi.action.UpdateUser;
import org.zizi.dao.UserDao;
import org.zizi.manager.LoginManager;
import org.zizi.manager.Managers;
import org.zizi.manager.impl.DaoManager;
import org.zizi.modal.User;

public class UserActionService implements ActionService
{
	public void UpdateUser(UpdateUser action) throws Exception
	{
		UserDao dao = Managers.get(DaoManager.class).getDao(UserDao.class);
		
		User u = dao.getUser(action.getId());
		if (u == null) return;
		
		if (UpdateUser.MAINTAIN_CREDIT.equals(action.getType()))
			u.setMaintainCredit(!u.isMaintainCredit());
		else if (UpdateUser.EMPOWER_USER.equals(action.getType()))
			u.setEmpowerUser(!u.isEmpowerUser());
		else if (UpdateUser.MAINTAIN_TYPE.equals(action.getType()))
			u.setMaintainType(!u.isMaintainType());
		
		dao.addOrUpdateUser(u);
		
		LoginManager lm = Managers.get(LoginManager.class);
		User x = lm.getUserByLogin(u.getLogin());
		
		if (x != null)
		{
			x.setMaintainCredit(u.isMaintainCredit());
			x.setEmpowerUser(u.isEmpowerUser());
			x.setMaintainType(u.isMaintainType());
		}
	}
}
